/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tiebreak.UI;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import tiebreak.BL.EasyException;
import tiebreak.BL.MemberWorker;

/**
 *
 * @author AngelwingedDevil
 */
public class AddMemberWindow extends JFrame
{

    private MemberForm form;
    private JLabel error;

    public AddMemberWindow()
    {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Add new Member");
        setSize(getPreferredSize());
        setResizable(false);
        setVisible(true);
    }

    private void initComponents()
    {
        form = new MemberForm();
        JPanel buttonPanel = new JPanel();
        ActionListener addAl = new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                register();
            }
        };
        ActionListener cancelAl = new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        };
        JButton add = new JButton("Add");
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(cancelAl);
        add.addActionListener(addAl);
        error = new JLabel("Current Errors: NONE");
        getContentPane().setLayout(new GridBagLayout());
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(add);
        buttonPanel.add(cancel);

        GridBagConstraints g = new GridBagConstraints();
        g.weightx = 0;
        g.weighty = 0;
        g.gridx = 0;
        g.gridy = GridBagConstraints.RELATIVE;
        g.insets = new Insets(TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE);
        g.fill = GridBagConstraints.NONE;
        getContentPane().add(form, g);
        g.fill = GridBagConstraints.BOTH;
        g.insets = new Insets(0, TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(buttonPanel, g);
        g.insets = new Insets(0, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(error, g);
    }

    private void register()
    {

        if (form.isFormValid())
        {
            try
            {
                MemberWorker mw = new MemberWorker();
               int id = mw.addMember(form.getFirstName(), form.getLastName(), form.getAddress(), form.getZip(), form.getCpr(), form.getPhone(), form.getMail(), form.getMemberType().getID(), form.elite(), form.tennisSchool(), form.isCoach());
                JOptionPane.showMessageDialog(null, form.getFirstName() + " Was Added to the database with the id: "+id, "Registration Complete", JOptionPane.INFORMATION_MESSAGE);
                dispose();
            }
            catch (EasyException e)
            {
                new TieBreakErrorHandling(e);
            }

        }
        else
        {
            error.setText(form.getInValidMessage());
            setSize(getPreferredSize());
            pack();
        }
    }
}
